<?php

namespace common\library;

class FFmpeg
{

    /**
     * 獲取視頻信息
     * @param $resources
     * @return mixed
     */
    public function getVideoInfo($resources)
    {
        exec('ffprobe -v quiet -print_format json -show_format -show_streams ' . $resources, $output);

        $result = '';
        foreach ($output as $o) {
            $result .= trim($o);
        }

        return json_decode($result, true);
    }

    /**
     * 視頻生成圖片
     * @param $resources
     * @param $savePath
     * @param int $width
     * @param int $height
     * @param float|int $second
     */
    public function generateImage($resources, $savePath, $width = 100, $height = 100, $second = 1)
    {
        exec('ffmpeg -i ' . $resources . ' -y -f image2 -t ' . $second / 1000 . ' -s ' . $width . 'x' . $height . ' ' . $savePath, $output, $return_val);
    }

}